<template>
	<view class="uni-container">
		<uni-forms ref="form" :model="formData" validate-trigger="submit" err-show-type="toast">

			<uni-forms-item name="name" label="收货人" required>
				<uni-easyinput placeholder="收货人" v-model="formData.name" trim="both"></uni-easyinput>
			</uni-forms-item>

			<uni-forms-item name="mobile" label="手机号" required>
				<uni-easyinput placeholder="手机号码，加密存储" v-model="formData.mobile" trim="both"></uni-easyinput>
			</uni-forms-item>


			<uni-forms-item name="area_code" label="省市区" required>
				<uni-data-picker self-field="code" parent-field="parent_code" v-model="formData.area_code"
					collection="opendb-city-china" orderby="value asc"
					field="code as value, name as text, eq(type, 2) as isleaf"></uni-data-picker>
			</uni-forms-item>
			<uni-forms-item name="address" :labelWidth="80" label="详细住址" required>
				<uni-easyinput placeholder="省市区后面的详细住址，包含街道小区房间号" v-model="formData.address"
					trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="email" label="邮箱">
				<uni-easyinput placeholder="邮箱" v-model="formData.email" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="is_default" label="默认地址">
				<switch @change="binddata('is_default', $event.detail.value)" :checked="formData.is_default"></switch>
			</uni-forms-item>
			<view class="uni-button-group">
				<button type="primary" class="uni-button" @click="submit">提交</button>
			</view>
			<uni-forms-item name="user_id" label="">

			</uni-forms-item>
		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
	} from '../../js_sdk/validator/uni-id-address.js';

	const db = uniCloud.database();
	const dbCollectionName = 'uni-id-address';

	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.indexOf(key) > -1) {
				result[key] = validator[key]
			}
		}
		return result
	}



	export default {
		data() {
			let formData = {
				"user_id": "",
				"name": "",
				"alias": "",
				"mobile": "",
				"mobile_area_code": "",
				"province_code": "",
				"city_code": "",
				"area_code": "",
				"address": "",
				"formatted_address": "",
				"zip_code": "",
				"email": "",
				"is_default": false
			}
			return {
				formData,
				formOptions: {},
				rules: {
					...getValidator(Object.keys(formData))
				}
			}
		},
		onLoad(e) {
			if (e.id) {
				const id = e.id
				this.formDataId = id
				this.getDetail(id)
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {

			/**
			 * 验证表单并提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.validate().then((res) => {
					return this.submitForm(res)
				}).catch(() => {}).finally(() => {
					uni.hideLoading()
				})
			},

			/**
			 * 提交表单
			 */
			submitForm(value) {
				// 使用 clientDB 提交数据
				return db.collection(dbCollectionName).doc(this.formDataId).update(value).then((res) => {
					uni.showToast({
						icon: 'none',
						title: '修改成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				})
			},

			/**
			 * 获取表单数据
			 * @param {Object} id
			 */
			getDetail(id) {
				uni.showLoading({
					mask: true
				})
				db.collection(dbCollectionName).doc(id).field(
					"user_id,name,alias,mobile,mobile_area_code,province_code,city_code,area_code,address,formatted_address,zip_code,email,is_default"
				).get().then((res) => {
					const data = res.result.data[0]
					if (data) {
						this.formData = data

					}
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				}).finally(() => {
					uni.hideLoading()
				})
			}
		}
	}
</script>

<style>
	.uni-container {
		padding: 15px;
	}

	.uni-input-border,
	.uni-textarea-border {
		width: 100%;
		font-size: 14px;
		color: #666;
		border: 1px #e5e5e5 solid;
		border-radius: 5px;
		box-sizing: border-box;
	}

	.uni-input-border {
		padding: 0 10px;
		height: 35px;

	}

	.uni-textarea-border {
		padding: 10px;
		height: 80px;
	}

	.uni-button-group {
		margin-top: 50px;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		justify-content: center;
	}

	.uni-button {
		width: 184px;
	}
</style>